package com.casstime.ec.cloud.discount.services;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.casstime.commons.utils.IncrementIdCreator;
import com.casstime.commons.utils.StringUtil;
import com.casstime.ec.cloud.discount.dao.business.SupplierPromotionConfigMapper;
import com.casstime.ec.cloud.discount.dao.business.SupplierPromotionConfigRecordMapper;
import com.casstime.ec.cloud.user.spi.models.User;
import com.casstime.ec.cloud.values.business.SupplierPromotionConfig;
import com.casstime.ec.cloud.values.business.SupplierPromotionConfigRecord;
import com.casstime.ec.cloud.values.business.enums.PromotionConfigValueEnum;
import com.google.common.collect.Lists;

/**
 * 
 * ClassName: PromotionConfigService 
 * Function: (店铺促销规则配置实现类)
 * Reason: ADD REASON(可选).
 *  
 * @author hongcheng.wu  
 * 2018年4月17日 上午10:21:13
 * @version   
 * @since JDK 1.8
 */
@Service
public class PromotionConfigService {
	
	@Autowired
	private SupplierPromotionConfigMapper supplierPromotionConfigMapper;
	
	@Autowired
	private SupplierPromotionConfigRecordMapper supplierPromotionConfigRecordMapper;
	
	public int insertSupplierPromotionConfig(SupplierPromotionConfig supplierPromotionConfig, User user) {
		if (supplierPromotionConfig == null) {
			return 0;
		}
		if (StringUtil.isEmpty(supplierPromotionConfig.getConfigType())) {
			supplierPromotionConfig.setConfigType(PromotionConfigValueEnum.FULL_COUPONS.getIndex());
		}
		
		SupplierPromotionConfig promotionConfig = this.getSupplierPromotionConfig(supplierPromotionConfig.getProductStoreId(),
				supplierPromotionConfig.getConfigType());
		IncrementIdCreator idCreator = new IncrementIdCreator();
		SupplierPromotionConfigRecord record = new SupplierPromotionConfigRecord();
		record.setId(idCreator.getNextIncrementId());
		record.setProductStoreId(supplierPromotionConfig.getProductStoreId());
		record.setUserLoginId(user.getUserLoginId());
		record.setUserLoginName(user.getUserName());
		if (promotionConfig == null) {
			supplierPromotionConfig.setId(idCreator.getNextIncrementId());
			if ("Y".equals(supplierPromotionConfig.getConfigValue())) {
				record.setDescription("促销规则初始化为：可以叠加");
			} else {
				record.setDescription("促销规则初始化为：不可叠加");
			}
		} else if (promotionConfig.getId().equals(supplierPromotionConfig.getId()) 
				&& promotionConfig.getConfigType().equals(supplierPromotionConfig.getConfigType())
				&& !promotionConfig.getConfigValue().equals(supplierPromotionConfig.getConfigValue())) {
				if ("Y".equals(supplierPromotionConfig.getConfigValue())) {
					record.setDescription("修改规则为：可以叠加");
				} else {
					record.setDescription("修改规则为：不可叠加");
				}
		}
		
		if (StringUtil.isEmpty(record.getDescription())) {
			return 0;
		}
		supplierPromotionConfigMapper.insertSupplierPromotionConfig(supplierPromotionConfig);
		supplierPromotionConfigRecordMapper.insertSupplierPromotionConfigRecord(record);
		return 1;
		
		
	}
	
	public SupplierPromotionConfig getSupplierPromotionConfig(String productStoreId, String configType) {
		return supplierPromotionConfigMapper.getSupplierPromotionConfig(productStoreId, 
				configType == null ? PromotionConfigValueEnum.FULL_COUPONS.getIndex() : configType);
	}
	
	public List<SupplierPromotionConfig> getSupplierPromotionConfigList(List<String> productStoreIds, String promotionType) {
		List<SupplierPromotionConfig> resultList = Lists.newArrayList();
		if (CollectionUtils.isEmpty(productStoreIds)) {
			return resultList;
		}
		List<SupplierPromotionConfig> list = supplierPromotionConfigMapper.getSupplierPromotionConfigList(productStoreIds,
				promotionType == null ? PromotionConfigValueEnum.FULL_COUPONS.getIndex() : promotionType);
		if (CollectionUtils.isNotEmpty(list)) {
			resultList.addAll(list);
		}
		return resultList;
	}
	
	
	
	
	
	
	
}
